package org.yray.sip.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Enumeration;

@Slf4j
public class IpUtil {

    private static String LOCAL_IP;

    public IpUtil() {
    }

    private static String getWindows() {
        String winip = "winip";

        try {
            InetAddress ia = InetAddress.getLocalHost();
            winip = ia.getHostAddress().toString();
        } catch (UnknownHostException var2) {
            log.error("win>>> get ip has happened error");
        }

        return winip;
    }

    private static String getLinux() {
        String ip = null;

        try {
            Enumeration en = NetworkInterface.getNetworkInterfaces();

            do {
                NetworkInterface intf;
                String name;
                do {
                    if (!en.hasMoreElements()) {
                        return ip;
                    }

                    intf = (NetworkInterface)en.nextElement();
                    name = intf.getName();
                } while(!name.contains("ens") && !name.contains("eth") && (name.contains("docker") || name.contains("lo") || name.contains("vir")));

                Enumeration enumIpAddr = intf.getInetAddresses();

                while(enumIpAddr.hasMoreElements()) {
                    InetAddress inetAddress = (InetAddress)enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && !inetAddress.isSiteLocalAddress()) {
                        String ipaddress = inetAddress.getHostAddress().toString();
                        if (!ipaddress.contains("::") && !ipaddress.contains("0:0:") && !ipaddress.contains("fe80")) {
                            ip = ipaddress;
                            break;
                        }
                    }
                }
            } while(StringUtils.isEmpty(ip));
        } catch (Exception var7) {
            log.error("linux >>> get ip has happened error {}", var7.getMessage());
        }

        return ip;
    }

    public static String getLocalIpStr() {
        String ip;
        try {
            if (System.getProperties().getProperty("os.name").toLowerCase().indexOf("win") >= 0) {
                ip = getWindows();
            } else {
                ip = getLinux();
            }

            if (!StringUtils.isEmpty(ip)) {
                ip = ip.replace(".", "-");
            }
        } catch (Exception var2) {
            ip = "999";
        }

        return ip;
    }

    public static String getLocalIp() {
        if (StringUtils.isEmpty(LOCAL_IP)) {
            try {
                if (System.getProperties().getProperty("os.name").toLowerCase().indexOf("win") >= 0) {
                    LOCAL_IP = getWindows();
                } else {
                    LOCAL_IP = getLinux();
                }
            } catch (Exception var1) {
                LOCAL_IP = "0.0.0.0";
            }
        }

        return LOCAL_IP;
    }
}
